package com.feng.zshop.backend.service.impl;

import com.feng.zshop.backend.dao.ProductTypeDao;
import com.feng.zshop.backend.service.ProductTypeService;
import com.feng.zshop.common.constant.ProductTypeConstant;
import com.feng.zshop.common.exception.ProductTypeExistsException;
import com.feng.zshop.model.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeDao productTypeDao;

    /**
     * 查询所有的商品类型
     *
     * @return List<ProductType>
     */
    @Override
    public List<ProductType> findAllProductType() {
        return productTypeDao.findAllProductType();
    }

    /**
     * 根据商品名称查询商品信息
     *
     * @param name
     * @return ProductType
     */
    @Override
    public ProductType selectProductTypeByName(String name) {
        return productTypeDao.selectProductTypeByName(name);
    }

    /**
     * 根据商品id查询商品信息
     *
     * @param id
     * @return ProductType
     */
    @Override
    public ProductType selectProductTypeById(Integer id) {
        return productTypeDao.selectProductTypeById(id);
    }


    /**
     * 添加商品类型
     *
     * @param productTypeName
     * @throws ProductTypeExistsException
     */
    @Transactional
    @Override
    public void addProductType(String productTypeName) throws ProductTypeExistsException {
        if (this.selectProductTypeByName(productTypeName) != null) {
            throw new ProductTypeExistsException("商品名称已经存在");
        }
        productTypeDao.addProductType(productTypeName, ProductTypeConstant.STATUS_ENABLE);
    }

    /**
     * 根据name查询 productType
     *
     * @param productTypeName
     * @return
     */
    public ProductType findByName(String productTypeName) {
        return productTypeDao.findByName(productTypeName);
    }

    /**
     * 更具id 删除 productType
     *
     * @param id
     */
    @Transactional
    @Override
    public void removeProductTypeById(Integer id) {
        Long count = productTypeDao.removeProductTypeById(id);
    }

    /**
     * 更新商品名称
     *
     * @param id
     * @param name
     */
    @Transactional
    @Override
    public void updateProductName(Integer id, String name) throws ProductTypeExistsException {
        if (this.selectProductTypeByName(name) != null) {
            throw new ProductTypeExistsException("商品名称已存在");
        }
        productTypeDao.updateProductNameById(id, name);
    }

    /**
     * 更新商品状态
     *
     * @param id
     */
    @Transactional
    @Override
    public void updateProductTypeStatus(Integer id) {
        ProductType productType = this.selectProductTypeById(id);
        if (productType.getStatus() == ProductTypeConstant.STATUS_ENABLE) {
            productTypeDao.updateProductStatusById(id, ProductTypeConstant.STATUS_DISABLE);
        } else {
            productTypeDao.updateProductStatusById(id, ProductTypeConstant.STATUS_ENABLE);
        }
    }

    /**
     * 根据状态查找
     *
     * @return
     */
    @Override
    public List<ProductType> findProductTypeEnable() {
        return productTypeDao.listByStatus(ProductTypeConstant.STATUS_ENABLE);
    }
}
